package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {
    public static final String EMPLOYEE_IN_SESSION =
            "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSIONS_IN_SESSION =
            "EXPRESSIONS_IN_SESSION";

    //获取 session 对象
    public static HttpSession getSession() {
        //根据线程对象获取绑定在线程上请求对象与session对象等等
        ServletRequestAttributes attrs = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    // 往 session 存入登录用户
    public static void setCurrentEmployee(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, employee);
    }

    //从 session 获取登录用户
    public static Employee getCurrentEmployee() {
        return  (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    //从 session 获取登录用户的权限信息
    public static List<String> getExpressions() {
        return (List<String>) getSession().getAttribute(EXPRESSIONS_IN_SESSION);
    }

    //往 session 存入登录用户的权限信息
    public static void setExpressions(List<String> expressions) {
        getSession().setAttribute(EXPRESSIONS_IN_SESSION,expressions);
    }


//获取登录


}
